package com.zhj.test.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author zhj
 */
public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "hello world";
        // 创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\data_file\\log.txt");
        // 通过fileOutputStream 获取对应fileChannel
        // 这个fileChannel 真实类型是 FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 将str 放入
        byteBuffer.put(str.getBytes());
        // 读写切换
        byteBuffer.flip();
        // 写入Channel
        fileChannel.write(byteBuffer);

        fileOutputStream.close();
    }
}
